// pages/shop/index.js
const App = getApp()
const { API, Ubt, imageHash, User } = App.services
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasChooseAddress:false,
    latitude:'',
    longitude:'',
    shopAddress:'',
    mapName:'',
    shopName:'',
    nickName:'',
    chooseAddress:'',
    portraitUrl:'',
    portraitUrlMain:''
  },
  toAddress:function(){
    wx.navigateTo({
      url:'/pages/location/location'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var place = wx.getStorageSync('PLACE')
    if(place){
      this.setData({
        shopAddress:place.address,
        mapName:place.name
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  blurPhone: function (e) {
    this.setData({
      telephone:e.detail.value
    })
  },
  // blurBossIdentityCardNo:function(e){
  //   this.setData({
  //     bossIdentityCardNo:e.detail.value
  //   })
  // },
  bindNickName:function(e){
    this.setData({
      nickName:e.detail.value
    })
  },
  bindShopName:function(e){
    this.setData({
      shopName:e.detail.value
    })
  },
  chooseImage: function(e) {
    var that = this;
    var type =  e.currentTarget.dataset.type

    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        wx.showLoading({
          title: '正在上传图片',
        })
        //上传文件：3：2和1:1比例一起生成
        wx.uploadFile({
          url: 'http://localhost:9002/web_api/v2/file/upload11Ratio',
          filePath: res.tempFilePaths[0],
          name: 'file',
          success: function (response) {
            wx.hideLoading();
            var resData = JSON.parse(response.data)
            if (resData.status === 'success') {
              that.setData({
                portraitUrl:resData.result.key,
                portraitUrlMain:resData.result.url
              })

            }else{
              wx.showModal({
                title: '提示',
                content: '上传失败' + resData.message,
                showCancel: false
              })
            }


          },
          fail: function (e) {
            wx.hideLoading();
            console.log(e);
            wx.showModal({
              title: '提示',
              content: '上传失败' + JSON.stringify(e),
              showCancel: false
            })
          },
          complete: function () {
            wx.hideToast(); //隐藏Toast
          }
        })
        //上传文件结束

      }
    })
  },
  saveShopUser(){
    var _this = this
    if (!_this.data.shopName) {
      wx.showModal({
        title: '提示',
        content: '商铺名称不能为空',
        showCancel: false
      })
      return
    }
    if(!_this.data.telephone){
      wx.showModal({
        title: '提示',
        content: '手机号不能为空',
        showCancel: false
      })
      return


    }else{
      if (!(/^1[3456789]\d{9}$/.test(_this.data.telephone))) {
        wx.showModal({
          title: '提示',
          content: '手机号格式不正确',
          showCancel: false
        })
        return
      }

    }


    if (!_this.data.nickName) {
      wx.showModal({
        title: '提示',
        content: '法人姓名不能为空',
        showCancel: false
      })
      return
    }
    if (!_this.data.portraitUrl) {
      wx.showModal({
        title: '提示',
        content: '请上传店铺头像',
        showCancel: false
      })
      return
    }
    var place = wx.getStorageSync('PLACE')
    if (!place.latitude) {
      wx.showModal({
        title: '提示',
        content: '请选择地址',
        showCancel: false
      })
      return
    }
    var user = wx.getStorageSync('USER')
    var data = {
      name: _this.data.shopName,
      contactMobile: _this.data.telephone,
      headImage:_this.data.portraitUrlMain,
      address:_this.data.shopAddress,
      latitude:place.latitude,
      longitude:place.longitude,
      userId:user.userId
      // shopUserId:shopUser.shopUserId
    }
    API.registerShopInfo(data).then(res => {
      wx.showLoading({
        title:'申请中',
        mask:true
      })
      if (res.data.status === 'success') {
        wx.hideLoading();
        wx.showLoading({
          title:'开店成功'
        })
        setTimeout(function () {
          wx.hideLoading();
          wx.navigateBack()
        },2000)

      } else {

      }
    }).catch(error =>{
      wx.hideLoading()
    })
    // if (_this.data.bossIdentityCardNo=='') {
    //   wx.showModal({
    //     title: '提示',
    //     content: '法人身份证不能为空',
    //     showCancel: false
    //   })
    //   return
    // }else{
    //   if (!(/^^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]/.test(_this.data.bossIdentityCardNo))) {
    //     wx.showModal({
    //       title: '提示',
    //       content: '身份证格式不正确',
    //       showCancel: false
    //     })
    //   }
    //   return
    // }
  }
})
